#define  _CRT_SECURE_NO_WARNINGS 1
#include<iostream>

using namespace std;


class Solution {
public:
    int myAtoi(string str)
    {
        int ret = 0;
        int bndry = INT_MAX / 10;
        int sign = 1;
        int size = str.size();
        if (size == 0)
        {
            return 0;
        }
        int i = 0;
        while (str[i] == ' ')
        {
            if (++i == size)
            {
                return 0;
            }
        }

        if (str[i] == '-')
        {
            sign = -1;
        }
        for (int j = i+1; j < size; j++)
        {
            if (str[j] < '0' || str[j] > '9')
            {
                break;
            }
            if (ret > bndry * 10 || (ret == bndry * 10 && str[j] > '7'))
            {
                return sign = 1 ? INT_MAX : INT_MIN;
            }
            ret = ret * 10 + str[j] - '0';
        }
        return sign * ret;

    }
};

int main()
{
    string s1;
    s1 += "  -42";
    Solution().myAtoi(s1);

    return 0;
}